package com.wobito.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wobito.common.constants.BusinessException;
import com.wobito.common.constants.ErrorCode;
import com.wobito.common.constants.SysConstants;
import com.wobito.common.utils.DateUtils;
import com.wobito.common.utils.EncrypUtils;
import com.wobito.mapper.UserMapper;
import com.wobito.pojo.bean.User;
import com.wobito.pojo.dto.user.UserDTO;
import com.wobito.pojo.vo.user.AddUserVO;
import com.wobito.pojo.vo.user.UpdatePwdVO;
import com.wobito.pojo.vo.user.UpdateUserVO;
import com.wobito.pojo.vo.user.UserVO;
import com.wobito.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author smile
 * @description 针对表【t_user】的数据库操作Service实现
 * @createDate 2024-04-18 10:36:40
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserDTO> selectUserList(UserVO userVO) {
        List<UserDTO> userList=userMapper.selectUserList(userVO);
        userList = userList.stream().filter(u -> !u.getUsername().equals("admin")).collect(Collectors.toList());
        return userList;
    }

    @Override
    public int insertUser(AddUserVO addUserVO) {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, addUserVO.getUsername()).or().eq(User::getPhone, addUserVO.getPhone()));
        if (addUserVO.getUsername().equals("admin")) {
            throw new BusinessException(ErrorCode.FORBIDDEN, "不可创建admin账号");
        }
        if (ObjectUtil.isNotEmpty(user)) {
            throw new BusinessException(ErrorCode.REPEATPARAMS_ERROR, "用户名或手机号重复");
        }
        User user1 = BeanUtil.copyProperties(addUserVO, User.class);
        user1.setRoleId(addUserVO.getRole());
        user1.setPassword(EncrypUtils.getEncryPassword(addUserVO.getPassword()));
        return userMapper.insert(user1);
    }

    @Override
    public int updateUser(UpdateUserVO updateUserVO) {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, updateUserVO.getUsername()).or().eq(User::getPhone, updateUserVO.getPhone()));
        if (user.getUsername().equals("admin")) {
            throw new BusinessException(ErrorCode.FORBIDDEN, "不可更改为admin账号");
        }
        if (ObjectUtil.isNotEmpty(user) && !updateUserVO.getId().equals(user.getId())) {
            throw new BusinessException(ErrorCode.REPEATPARAMS_ERROR, "用户名或手机号重复");
        }
        User user1 = BeanUtil.copyProperties(updateUserVO, User.class);
        user1.setRoleId(SysConstants.PT_USER_ROLE);
        user1.setUpdateTime(DateUtils.getNowDate());
        return userMapper.updateById(user1);
    }

    @Override
    public int updatePwd(UpdatePwdVO updatePwdVO) {
        User user=userMapper.selectById(updatePwdVO.getId());
        if (EncrypUtils.matches(updatePwdVO.getOldPwd(),user.getPassword())){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"旧密码错误");
        }
        if (user.getPassword().equals(EncrypUtils.getEncryPassword(updatePwdVO.getNewPwd()))){
            throw new BusinessException(ErrorCode.REPEATPARAMS_ERROR,"新旧密码一致");
        }
        return userMapper.updateById(new User(updatePwdVO.getId(),EncrypUtils.getEncryPassword(updatePwdVO.getNewPwd()),DateUtils.getNowDate()));
    }
}